'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en', 'datetimepicker'], function ($) {
  return {
  
    init: function () {
      this.initPage();
      this.audit();
      this.batchAudit();
      this.upload();
      this.transfer();
      this.initDelete();
      this.out();
    },
  
    initPage: function () {
      
      $("#tractor-upload-result").modal('show');
      
      $('a.batch-audit').addClass('disabled');
      
      $(document).on('click', '#checkAll', function(e) {
        $('input[name="id"]').prop('checked', $('#checkAll').is(':checked'));
      });
      
      $(document).on('change', '#checkAll, input[name="id"]', function(e) {
        var ids = $('input[name="id"]:checked').val();
        if (ids) {
          $('a.batch-audit').removeClass('disabled');
        } else {
          $('a.batch-audit').addClass('disabled');
        }
      });
      
    },
    
    audit: function () {
      $(document).on("shown.bs.modal", "#audit", function (e) {
      	$(e.currentTarget).find("form").shiftform();
      });
    },
    
    batchAudit: function() {
      
      $(document).on('shown.bs.modal', '#batch-audit', function(e) {

        var idArray = new Array();
        $('input[name="id"]:checked').each(function() {
          var id = $(this).val();
          idArray.push(id);
        });
        $('input[name="ids"]').val(idArray);
        
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform();
        
      });
      
    },
    
    transfer: function() {
      $(document).on("shown.bs.modal", "#transfer", function (e) {
        $(e.currentTarget).find("form").shiftform()
      });
    },
    
    out: function () {
      $(document).on("shown.bs.modal", "#out", function (e) {
        $(e.currentTarget).find("form").shiftform()
      });
    },
    
    upload: function() {
      
      $(document).on('shown.bs.modal', '#upload', function(e) {
        
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
        
        document.getElementById("cover").onchange = function () {
          document.getElementById("coverFile").value = this.value;
        }
        
      })
      
    },
    
    initDelete: function() {
      $(document).on('shown.bs.modal', '#delete', function(e) {
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform();
      })
    }

  }
})